//
//  ZHGViewController.m
//  悦享
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "ZHGViewController.h"
#import "SecViewController.h"
#import "PicCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "LeftViewTableViewCell.h"
#import "MovieViewController.h"
#import "LeftSlideViewController.h"
#import "HGLeftViewController.h"
@interface ZHGViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UICollectionView *collection;/**<  主页面的collection */
@property (nonatomic, retain) NSMutableArray *dataArr;/**<  数据源数组 */

@property (nonatomic, retain) UILabel *titleLabel;/**<  王牌强档 */

@property (nonatomic, retain) UIImageView *LowImageView;/**<  底层毛玻璃图片 */
@property (nonatomic, assign) NSInteger isPage;

@property (nonatomic, retain) UIButton *shareButton;/**<  分享按钮 */
@property (nonatomic, retain) UIButton *homeButton;/**<  主页面button */
@property (nonatomic, retain) UIView *leftView;/**<  侧边弹出的主页view */
@property (nonatomic, assign) BOOL selectLeftView;/**<  已经出现左边view */
@property (nonatomic, retain) UITableView *leftTableView;/**<  左边弹出的菜单tableview */

@property (nonatomic, retain) NSIndexPath *isCell;/**<  tableview出现了第几个cell */

@property (nonatomic, retain) NSArray *arrTitle;/**<  tableView列表数据 */
@end

@implementation ZHGViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        _leftView.frame = CGRectMake(-150, 0, 150, SREEN_HEIGHT);
        
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        [_leftView removeFromSuperview];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    [self createLowImageView];
    [self createTitle];
    
    
    
    [self createCollection];
    [self dataHandel];
    
    [self createItem];
    
    
    
    self.arrTitle = [NSArray arrayWithObjects:@"精选", @"综艺", @"电视剧", @"电影", @"动漫", @"少儿",  @"会员", @"音乐" , @"逗爸", nil];
    
    
    
}



/**<  创建按钮 */
- (void)createItem{
    //添加分享button
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _shareButton.frame = CGRectMake(SREEN_WIDTH - 80, 30, 40, 40);
    _shareButton.backgroundColor = [UIColor clearColor];
    [_shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang-3.png"] forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_shareButton];
    
    //添加homebutton
    self.homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _homeButton.frame = CGRectMake(40, 30, 40, 40);
    _homeButton.backgroundColor = [UIColor clearColor];
    [_homeButton setImage:[UIImage imageNamed:@"iconfont-home.png"] forState:UIControlStateNormal];
    
    [_homeButton addTarget:self action:@selector(homeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_homeButton];
    
    
}
///分享按钮县级方法
- (void)shareButtonAction:(UIButton *)button{
    
}
/**<  创建左边弹出的view */
- (void)createCheView{
    
    self.leftView = [[UIView alloc] initWithFrame:CGRectMake(-150, 0, 150, SREEN_HEIGHT)];
    _leftView.backgroundColor = [UIColor colorWithRed:0.3 green:0.32 blue:0.33 alpha:0.2];
    
    [self.view addSubview:_leftView];
    
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    effectView.frame = CGRectMake(0, 0, 150, SREEN_HEIGHT);
    [_leftView addSubview:effectView];
    
    
}
///当点击的时候放左边的view收回
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.3 animations:^{
        _leftView.frame = CGRectMake(-150, 0, 150, SREEN_HEIGHT);
       
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        [_leftView removeFromSuperview];
    }];
}
///homeButton点击方法
- (void)homeButtonAction:(UIButton *)button{
    _selectLeftView = YES;
    [self createCheView];
    [UIView animateWithDuration:0.3 animations:^{
        _leftView.frame = CGRectMake(0, 0, 150, SREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [self createTableView];
    }];
}

///添加底层毛玻璃图片
- (void)createLowImageView{
    
    self.LowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, SREEN_HEIGHT)];
    
    _LowImageView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_LowImageView];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    effectView.frame = CGRectMake(0, 0, SREEN_WIDTH, SREEN_HEIGHT);
    [_LowImageView addSubview:effectView];
}

///创建左边弹出的tableView
- (void)createTableView{
    
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 150, SREEN_HEIGHT)];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _leftTableView.backgroundColor = [UIColor clearColor];
    [_leftView addSubview:_leftTableView];
    
    [_leftTableView registerClass:[LeftViewTableViewCell class] forCellReuseIdentifier:@"LeftViewTableViewCell"];
    
}
///tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrTitle.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeftViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftViewTableViewCell"];
    self.isCell = indexPath;
    
    
    
    cell.titleLabel1.text = _arrTitle[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.layer.transform = CATransform3DMakeRotation(2, 0, 1, 0);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeRotation(0, 0, 0, 0);
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieViewController *movie = [[MovieViewController alloc] init];
    
    if (indexPath.row == 0) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1001&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=6cfac9bdf10a590961ec1e3ee4a0360a&ticket=&type=normal";
        movie.lookStr = @"精选";
    }
    if (indexPath.row == 2) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1004&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=24abe737313fbeccc160d99917a89ae9&ticket=&type=normal";
        movie.lookStr = @"查看全部电视剧 >>";
    }
    if (indexPath.row == 3) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1005&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=01d463fc5ae43c60f892fcfb8bc15bf3&ticket=&type=normal";
        
        movie.lookStr = @"查看全部电影 >>";
    }
    if (indexPath.row == 4) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1006&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=b5eb07a59ed675533dc787dfe4e34859&ticket=&type=normal";
        movie.lookStr = @"查看全部动漫 >>";
    }
    if (indexPath.row == 5) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1021&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=c82947f4a73fc5e2448e8b84313ae68b&ticket=&type=normal";
        movie.lookStr = @"查看全部内容 >>";
    }
    if (indexPath.row == 6) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1007&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=8ab8c5eca307fd4daa96859e77ccce70&ticket=&type=normal";
        movie.lookStr = @"查看全部内容 >>";
    }
    if (indexPath.row == 7) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1026&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=f95b1d00cea1877b9666e0bb57acb02d&ticket=&type=normal";
        movie.lookStr = @"查看全部内容 >>";
    }
    
    if (indexPath.row == 8) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1007&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=6f3ffb07e1e6cbf17027f7f616b2b5c1&ticket=&type=normal";
        movie.lookStr = @"查看全部内容 >>";
    }
    
    if (indexPath.row == 1) {
        movie.url = @"http://mobile.api.hunantv.com/channel/getDetail?appVersion=4.6.3&channelId=1003&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=159598563da78b4549710a5f01713ad7&ticket=&type=normal";
        movie.lookStr = @"查看全部综艺 >>";
    }
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:movie animated:YES];
}
///创建标题
- (void)createTitle{
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((SREEN_WIDTH - 200)/2, 30, 200, 20)];
    
    _titleLabel.text = @"王牌强档";
    _titleLabel.font = [UIFont systemFontOfSize:19];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    
    [self.view addSubview:_titleLabel];
}
/**<  创建collectionView */
- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(SREEN_WIDTH, SREEN_HEIGHT - 240);
  
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 120, SREEN_WIDTH, SREEN_HEIGHT - 240) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.pagingEnabled = YES;
    _collection.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_collection];
    
    [_collection registerClass:[PicCollectionViewCell class] forCellWithReuseIdentifier:@"PicCollectionViewCell"];
    
}

#pragma mark-collection代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PicCollectionViewCell" forIndexPath:indexPath];
    
    TVModel *model = _dataArr[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.model = model;
   
    [UIView animateWithDuration:0.5 animations:^{
        _LowImageView.alpha = 0.3;
        
    }];
    
    [self performSelector:@selector(setImageView) withObject:nil afterDelay:1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_selectLeftView == YES) {
        [UIView animateWithDuration:0.5 animations:^{
            _leftView.frame = CGRectMake(-150, 0, 150, SREEN_HEIGHT);
            [_leftView removeFromSuperview];
        }];
        _selectLeftView = NO;
    }else{
        TVModel *model = _dataArr[indexPath.row];
        
        SecViewController *sec = [[SecViewController alloc] init];
        sec.url = model.playUrl;
        self.hidesBottomBarWhenPushed = YES;
        [self presentViewController:sec animated:YES completion:^{
            
        }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == _collection) {
        self.isPage = _collection.contentOffset.x / SREEN_WIDTH;
        ///当滑动的时候放左边的view收回
        [UIView animateWithDuration:0.5 animations:^{
            _leftView.frame = CGRectMake(-150, 0, 150, SREEN_HEIGHT);
             [_leftTableView removeFromSuperview];
        }];
        
    }
    
    
}

- (void)setImageView{
  
    TVModel *model = _dataArr[_isPage];
    
    [UIView animateWithDuration:2 animations:^{
       
        [_LowImageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:nil];
        _LowImageView.alpha = 1;
    }];
}


/** 数据处理 */
- (void)dataHandel{
    
  [APPTools GETWithURL:@"http://mobile.api.hunantv.com/channel/getWPDetail?appVersion=4.6.3&device=iPhone&mac=d46e513335b7f3a1657845d54a24bc3d2ced0c7f&osType=ios&osVersion=9.200000&seqId=97a7ce3ec206152d516aab575e1f5ad8&ticket=" par:nil success:^(id responseObject) {
      
      NSDictionary *result = (NSDictionary *)responseObject;
      NSArray *dataArr = result[@"data"];
      
      self.dataArr = [TVModel baseModelHandleWithArr:dataArr];
      
      NSLog(@"%@", _dataArr);
      
      [_collection reloadData];
      
  } filed:^(NSError *error) {
      
  }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
